/*
 *	推箱子
 *	20*20 的地图.
 *	整体思路,定义全局数组,//墙壁0,地面1,人2,箱子3,胜利点4.
 */
#include"box.h"
#include"map.h"

int main() {
	int direction = 0;
	map();
	makemap(map[][], 20, 20);	//画地图
	//读取人的位置,人的位置给manX,manY
	int manX = whereisman(map[][], 20, 20, 0), manY = whereisman(map[][], 20, 20, 1);
	while (isWin(map[][], 20, 20) == 0) {
		printf("输入方向(上0左1下2右3):");
		scanf("%d", &direction);
		move(map[][], 20, 20, isMeet(map[][], 20, 20, direction), direction);
		if (move(map[][], 20, 20, isMeet(map[][], 20, 20, direction)) == 1) {
			makemap();
		}
	}
	printf("恭喜你胜利了!\n");
	return 0;
}

int whereisman(int map[][], int sizer, int sizec, int num) {
	for (int i = 0; i < sizer; i++) {
		for (int j = 0; j < sizec; j++) {
			if (map[i][j] == 2) {
				if (num = 0) {
					return i;
				}
				else {
					return j;
				}
			}
		}
	}
}

void makemap(int map[],int sizer, int sizec) {
	for (int i = 0; i < sizer; i++) {
		for (int j = 0; j < sizec; j++) {
			if (map[i][j] == 0) {
				printf("*");
			}
			else if (map[i][j] == 1) {
				printf(" ");
			}
			else if (map[i][j] == 2) {
				printf("O");
			}
			else if (map[i][j] == 3) {
				printf("X");
			}
			else {
				printf("!");
			}
		}
		printf("\n");
	}
}

int isWin(int map[][], int sizer, int sizec) {
	if (map[1][1] == 3 && map[1][2] == 3) {			//胜利点都变成箱子,胜利了.
		return 1;
	}
	else {
		return 0;
	}
}

int isMeet(int map[][], int sizer, int sizec, int direction) {
	int addX = 0, addY = 0;
	switch (direction) {
		case 0 :
			addX = 0, addY = -1;
			break;
		case 1:
			addX = -1, addY = 0;
			break;
		case 2:
			addX = 0, addY = 1;
			break;
		case 3:
			addX = 1, addY = 0;
			break;
	}
	int x = 0, y = 0;
	x = whereisman(map[][], 20, 20, 0), y = whereisman(map[][], 20, 20, 1);
	if (map[x + addX][y + addY] == 1 || map[x + addX][y + addY] == 4) {
		return 0;	//地面或胜利点,只有人移动.胜利点优先级比人和箱子低.
	}
	else if (map[x + addX][y + addY] == 0) {
		return 1;	//墙壁,不能移动
	}
	else if (map[x + addX][y + addY] == 3) {
		if (map[x + 2 * addX][y + 2 * addY] == 0 || map[x + 2 * addX][y + 2 * addY] == 3) {
			return 2;		//箱子后面是墙或箱子,不能移动.
		}
		else {
			return 3;		//箱子后面是地面或胜利点,可以移动.
		}
	}
}

int move(int map[][], int sizer, int sizec, int isMeet, int direction) {
	int addX = 0, addY = 0;
	switch (direction) {
		case 0 :
			addX = 0, addY = -1;
			break;
		case 1:
			addX = -1, addY = 0;
			break;
		case 2:
			addX = 0, addY = 1;
			break;
		case 3:
			addX = 1, addY = 0;
			break;
	}
	int x = 0, y = 0;
	x = whereisman(map[][], 20, 20, 0), y = whereisman(map[][], 20, 20, 1);
	switch (isMeet) {
		cast 0:
			map[x][y] = 
	}
}
